package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {

        log.info("购物城数据：{}",shoppingCart);

        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);
        if (dishId!=null){
       queryWrapper.eq(ShoppingCart::getDishId,dishId);

        }
        else{
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart shoppingCartone = shoppingCartService.getOne(queryWrapper);

        if (shoppingCartone!=null){
            Integer number = shoppingCartone.getNumber();
            shoppingCartone.setNumber(number+1);
            shoppingCartService.updateById(shoppingCartone);

        }
        else
        {
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            shoppingCartone = shoppingCart;
        }
        return R.success(shoppingCartone);
    }

    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){

      log.info("查看购物车...");

      LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
      queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }
    @DeleteMapping("/clean")
    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());

        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车成功");
    }

    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart) {



        Long currentId = BaseContext.getCurrentId();
//        shoppingCart.setUserId(currentId);

        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);
        if (dishId!=null){
            queryWrapper.eq(ShoppingCart::getDishId,dishId);

        }
        else{
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart shoppingCartone = shoppingCartService.getOne(queryWrapper);

        Integer number = shoppingCartone.getNumber();

        if (number>1){
            shoppingCartone.setNumber(number-1);
            shoppingCartService.updateById(shoppingCartone);
        }
        else
            if(number==1){
                shoppingCartService.remove(queryWrapper);
            }
        return R.success("删除成功");
    }
}
